package com.hkts.management.unattended.poundroom.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.unattended.poundroom.entity.YbMutualPovouch;
import com.hkts.management.unattended.poundroom.entity.YbMutualSovouch;
import com.hkts.management.unattended.poundroom.mapper.YbMutualSovouchMapper;
import com.hkts.management.unattended.poundroom.service.IYbMutualSovouchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-10-13
 */
@Service
@Transactional
public class YbMutualSovouchServiceImpl extends ServiceImpl<YbMutualSovouchMapper, YbMutualSovouch> implements IYbMutualSovouchService {


    @Autowired
    private YbMutualSovouchMapper ybMutualSovouchMapper;

    @Override
    public IPage<YbMutualSovouch> selectPage(Page<YbMutualSovouch> page, YbMutualSovouch ybMutualSovouch){
        QueryWrapper<YbMutualSovouch> query=Wrappers.query();
        return ybMutualSovouchMapper.selectPage(page,query);
        }


    @Override
    public List<YbMutualSovouch> selectList(YbMutualSovouch ybMutualSovouch){
        QueryWrapper<YbMutualSovouch> query=Wrappers.query();
        return ybMutualSovouchMapper.selectList(query);
        }
    public LocalDateTime getLastUpdateTime(){
        QueryWrapper<YbMutualSovouch> query = Wrappers.query();
        query.lambda().orderByDesc(YbMutualSovouch::getDoptTime);
        query.last("limit 1");
        List<YbMutualSovouch> ybMutualCompanies=ybMutualSovouchMapper.selectList(query);
        return ybMutualCompanies.size()>0?ybMutualCompanies.get(0).getDoptTime():null;
    }
    public void mysqlInsertOrUpdateBath(List<YbMutualSovouch> ybMutualCompanies){
        ybMutualSovouchMapper.mysqlInsertOrUpdateBath(ybMutualCompanies);
    }

    public YbMutualSovouch getBycSOCode(String code){
        QueryWrapper<YbMutualSovouch> query = Wrappers.query();
        query.lambda().eq(YbMutualSovouch::getcSOCode,code);
        return ybMutualSovouchMapper.selectOne(query);
    }


    @Override
    public List<Map<String,Object>> selectListByName(String name) {
        QueryWrapper<YbMutualSovouch> query = Wrappers.query();
        query.lambda().like(YbMutualSovouch::getcInvName,name).or().like(YbMutualSovouch::getcCusAbbName,name);
        List<YbMutualSovouch> ybMutualSovouches = ybMutualSovouchMapper.selectList(query);
        List<Map<String,Object>> collect = ybMutualSovouches.stream().map(this::assemblyybMutualSovouchData).collect(Collectors.toList());
        return collect;
    }

    private Map<String,Object> assemblyybMutualSovouchData(YbMutualSovouch ybMutualSovouches) {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("orderCode",ybMutualSovouches.getcSOCode());
        resultMap.put("providerOrClient",ybMutualSovouches.getcCusAbbName());
        resultMap.put("cInvName",ybMutualSovouches.getcInvName());
        resultMap.put("estimatedShipDate",ybMutualSovouches.getDmodifyTime());
        resultMap.put("model",ybMutualSovouches.getcInvStd());
        resultMap.put("orderQuantity",ybMutualSovouches.getiQuantity());
        resultMap.put("noOrAddUp","");
        return resultMap;
    }

}
